﻿using Ldt.DataEngine.LogHandle;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using LdtJudge.DataEngine.Model.CommModel;
using MQModelHandleLibaryCore.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using static Ldt.DataEngine.LogHandle.LogHandle;

namespace LdtJudge.DataEngine.Server
{
    public class RealizationClass
    {
        private LogShow _uishow;
        private LogShowFontStyle _uilogShowFontStyle;


        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="logType">消息类型</param>
        /// <param name="text">日志内容</param>
        private void DoSetLogtype(string text, LogMessageType logType = LogMessageType.Warn)
        {
            LogHType log = LogHType.Info;
            foreach (LogHType typ in Enum.GetValues(typeof(LogHType)))
            {
                if (((int)logType) == ((int)typ))
                {
                    log = typ;
                    break;
                }
            }
            //写入记录日志
            LogHandle.GetInitLogHandle.LogWrite(log, text);
            if (_uishow != null)
            {
                this._uishow(text, logType);
            }


        }

        /// <summary>
        /// 日志显示
        /// </summary>
        public LogShow LogShow
        {
            get { return DoSetLogtype; }
            set { _uishow = value; }
        }

        public enum FontStyles
        {
            defult,
            style1,
            style2,
            style3,
            style4,
            style5,
            style6,
            style7,
            style8,
            style9,
            style10
        }
        /// <summary>
        /// MQ链接对象
        /// </summary>
        public ConnectionConfig Connection { get; set; }

        #region 分析文书实现方法


        /// <summary>
        /// 当前运行模块
        /// </summary>
        /// <param name="moduleName"></param>
        /// <param name="processValue"></param>
        public void SetModuleProcess(string moduleName, int processValue, MainInputParameterModel inputParameterModel)
        {

        }
        /// <summary>
        /// 执行完成模块
        /// </summary>
        /// <param name="mdoelstring"></param>
        /// <param name="type"></param>
        public void ModularModelSet(string mdoelstring, string type)
        {

        }
        /// <summary>
        /// 执行完成模块
        /// </summary>
        /// <param name="mdoelstring"></param>
        /// <param name="type"></param>
        public void ModularModelSet(string mdoelstring, int type, MainInputParameterModel inputParameterModel)
        {
            int moduleIndex = 0;
            int currentIndex = 0;
            foreach ( var  config in inputParameterModel.configModelList)
            {
                moduleIndex++;
                if (config.Code.ToString().ToLower()==mdoelstring.Split('_')[0].ToLower())
                {
                    currentIndex = moduleIndex;
                    if (type < 100)
                    {
                        ProcessShow("", (currentIndex - 1) * 100 / inputParameterModel.configModelList.Count, inputParameterModel);
                      
                    }
                    else if (type == 100)
                    {

                        ProcessShow("", currentIndex * 100 / inputParameterModel.configModelList.Count, inputParameterModel);
                    }
                }
            }
        }
        /// <summary>
        /// 进度
        /// </summary>
        /// <param name="fileId"></param>
        /// <param name="nvalue"></param>
        public void ProcessShow(string fileId, int nvalue, MainInputParameterModel inputParameterModel)
        {
            if (inputParameterModel != null)
            {
                if (nvalue > 100)
                {
                    nvalue = 100;
                }
                HandleProgressModel handleProgressModel = new HandleProgressModel();
                handleProgressModel.MsgId = inputParameterModel.MsgID;
                handleProgressModel.HandleType = "2";
                CaseInfo caseInfo = new CaseInfo();
                caseInfo.OutRealtId = inputParameterModel.CaseID;
                caseInfo.CaseId = inputParameterModel.CaseID;
                caseInfo.CaseCause = inputParameterModel.CaseCause;
                caseInfo.CaseType = inputParameterModel.CaseType;
                handleProgressModel.ProgressValue = nvalue.ToString();
                handleProgressModel.CaseInfo = caseInfo;
                DocInfol docInfol = new DocInfol();
                docInfol.SFID = inputParameterModel.SF_ID;
                docInfol.SourceCode = inputParameterModel.DocID;
                handleProgressModel.DocInfo = docInfol;

                string message = JsonConvert.SerializeObject(handleProgressModel);
                //进行发布消息
                MQModelHandleLibaryCore.PublishSubscribeHandle.PublishSubscribeSend.GetPublishSubscribeSendInstance("HandleProgressExchange").PublishMessage("HandleProgress", message);
            }

        }



        /// <summary>
        /// 进度条显示
        /// </summary>
        /// <param name="fileIndex">文件序号</param>
        /// <param name="totalNumb">总文件数</param>
        /// <param name="filepath">文件路径</param>
        /// <param name="processValue">进度条值</param>
        public void ProcessShowP(int fileIndex, int totalNumb, string filePath, int nvalue, BufferModel bufferModel, int processValue)
        {
            if (bufferModel != null)
            {
                processValue = nvalue;
                if (processValue > 100)
                {
                    processValue = 100;
                }
                HandleProgressModel handleProgressModel = new HandleProgressModel();
                handleProgressModel.HandleType = bufferModel.HandleType;
                handleProgressModel.MsgId = bufferModel.MsgId;

                CaseInfo caseInfo = new CaseInfo();
                caseInfo.OutRealtId = bufferModel.OutRelatID;
                handleProgressModel.ProgressValue = processValue.ToString();
                handleProgressModel.CaseInfo = caseInfo;
                DocInfol docInfol = new DocInfol();
                docInfol.FileId = bufferModel.FilePath;
                docInfol.SourceCode = bufferModel.SourceCode;
                handleProgressModel.DocInfo = docInfol;
                
                string message = JsonConvert.SerializeObject(handleProgressModel);
                //进行发布消息
                MQModelHandleLibaryCore.PublishSubscribeHandle.PublishSubscribeSend.GetPublishSubscribeSendInstance("HandleProgressExchange").PublishMessage("HandleProgress", message);
            }
        }
        #endregion

        /// <summary>
        /// 显示后台日志wxlu
        /// </summary>
        /// <param name="log"></param>
        public void ShowMessage(string log, LogMessageType messagetype = LogMessageType.Info)
        {
            LogShowFontStyle(log, FontStyles.defult);
        }
        /// <summary>
        /// 日志显示有字体样式
        /// </summary>
        /// <param name="logInfo">日志</param>
        /// <param name="fontStyles">日志样式</param>
        public void LogShowFontStyle(string logInfo, FontStyles fontStyles)
        {

        }
    }
}
